library ieee;
use ieee.std_logic_1164.all;

use work.sec_counter_pkg.all;
use work.leds_controller_pkg.all;
use work.ssd_controller_pkg.all;

entity top is
    generic (
        CYCLES_SEC : integer := 100e6; -- 1/10ns
        CYCLES_7_SEGMENT_REFRESH : integer := 16e5; -- 16ms/10ns
        MAX_SECS_LOG2 : integer := 16; -- 16-bit leds
        LED_LOW : integer := 0;
        LED_HIGH : integer := 15;
        SEG_LOW : integer := 0;
        SEG_HIGH : integer := 3;
        SEG_MIN : integer := 0;
        SEG_MAX : integer := 6
    );
    port (
        -- general control signals
        clk : in std_logic;
        res : in std_logic;
        -- led controller signals
        led : out std_logic_vector(LED_HIGH downto LED_LOW);
        -- ssd controller signals
        seg_n : out std_logic_vector(SEG_MAX downto SEG_MIN);
        dp_n : out std_logic;
        an_n : out std_logic_vector(SEG_HIGH downto SEG_LOW);
        -- pmod port JC signals
        JC : out std_logic_vector(7 downto 0)
    );
end top;

architecture Behavioral of top is

    signal tick : std_logic := '0';
    signal cnt_secs : integer := 0;
    
    signal pmod_cs     : std_logic := '0';
    signal pmod_mosi   : std_logic := '0';
    signal pmod_sck    : std_logic := '0';
    signal pmod_dc     : std_logic := '0';
    signal pmod_res    : std_logic := '0';
    signal pmod_vccen  : std_logic := '0';
    signal pmod_pmoden : std_logic := '0';

begin

    -- pmod port JC assignments
    JC(0) <= pmod_cs;
    JC(1) <= pmod_mosi;
    JC(2) <= '0'; -- (NC) means not connected
    JC(3) <= pmod_sck;
    JC(4) <= pmod_dc;
    JC(5) <= pmod_res;
    JC(6) <= pmod_vccen;
    JC(7) <= pmod_pmoden;

    sec_counter_inst : sec_counter
    generic map (
        CYCLES_SEC => CYCLES_SEC,
        MAX_SECS_LOG2 => MAX_SECS_LOG2
    )
    port map (
        -- incoming control signals
        clk => clk,
        res => res,

        -- outgoing tick signal
        tick => tick,
        cnt_secs => cnt_secs
    );

    leds_controller_inst : leds_controller
    generic map (
        LED_LOW => LED_LOW,
        LED_HIGH => LED_HIGH
    )
    port map (
        clk => clk,
        res => res,

        tick => tick,
        cnt_secs => cnt_secs,

        led => led
    );

    ssd_controller_inst : ssd_controller
    generic map (
        CYCLES_TOTAL => CYCLES_7_SEGMENT_REFRESH,
        CYCLES_PER_ANODE => CYCLES_7_SEGMENT_REFRESH / 4,
        SEG_LOW => SEG_LOW,
        SEG_HIGH => SEG_HIGH,
        SEG_MIN => SEG_MIN,
        SEG_MAX => SEG_MAX
    )
    port map (
        clk => clk,
        res => res,

        tick => tick,
        cnt_secs => cnt_secs,

        seg_n => seg_n,
        dp_n => dp_n,
        an_n => an_n
    );

    /*
    oled_controller_inst : oled_controller
    generic map (
    )
    port map (
        clk => clk,
        res => res,

        pmod_cs     => pmod_cs,
        pmod_mosi   => pmod_mosi,
        pmod_sck    => pmod_sck,
        pmod_dc     => pmod_dc,
        pmod_res    => pmod_res,
        pmod_vccen  => pmod_vccen,
        pmod_pmoden => pmod_pmoden
    );
    */

end Behavioral;
